package kr.ac.skhu.m.data;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.Source;

import kr.ac.skhu.m.value.NoticeListValue;

public class BoardData {
	
	private String url;
	private String type;
	private int intTypeCurPage;
	private int intTypeTotalPage;
	
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public int getIntTypeCurPage() {
		return intTypeCurPage;
	}
	public void setIntTypeCurPage(int intTypeCurPage) {
		this.intTypeCurPage = intTypeCurPage;
	}
	public int getIntTypeTotalPage() {
		return intTypeTotalPage;
	}
	public void setIntTypeTotalPage(int intTypeTotalPage) {
		this.intTypeTotalPage = intTypeTotalPage;
	}
	public BoardData() {
	
	}
	public BoardData(String url) {
		this.url = url;
	}
	public BoardData(String url, String type) {
		this.url = url;
		this.type = type;
	}
	
	public List<NoticeListValue> getNoticeListValue() throws Exception {
		
		//해당 URL 페이지를 가져온다.
		Source source = new Source(new URL(url));

		//메소드 찾기를 위해 시작부터 끝까지 태그들만 parse 한다 (?)
		source.fullSequentialParse();
		
		/**
		 * 목록 생성
		 */
		List<NoticeListValue> valueList = new ArrayList<NoticeListValue>();
		
		List<Element> board_list = source.getAllElementsByClass("left15");
		int board_list_length = board_list.size();
		
		String text = "";
		String idx = "";
		String name = "";
		String todayDate = "";
		
		String href = "";
		int start = 0;  
		int end = 0; 

		int tdIndex = 3;
		
		if(url.contains("10016")){
			tdIndex = tdIndex - 1;
		}
		
		for(int i=0; i<board_list_length; i++){
			Element element = board_list.get(i).getAllElements(HTMLElementName.A).get(0);
			text = element.getTextExtractor().toString();
			href = element.getAttributeValue("href");
			
			
			
			Element tr = element.getParentElement().getParentElement().getParentElement();	
			name = tr.getChildElements().get(tdIndex).getTextExtractor().toString();
			todayDate = tr.getChildElements().get(tdIndex + 1).getTextExtractor().toString();
			
			start = href.indexOf("idx=") + 4;
			end = href.indexOf("&");

			idx = href.substring(start, end);

			valueList.add(new NoticeListValue(text, idx, name, todayDate));
		} 
		
		/**
		 * 답변게시판일 경우 답변내용
		 */
		
		/**
		 * 페이징 정보
		 */
		//현재 페이지 번호
		Element cur = source.getAllElementsByClass("cur").get(0);
		String curPage = cur.getTextExtractor().toString();
		this.intTypeCurPage = Integer.parseInt(curPage);
		
		//전체 페이지 수
		Element nextL = source.getAllElementsByClass("nextL").get(0);
		String nextLString = nextL.getAttributeValue("href");
		int nextLBegin = nextLString.indexOf("curpage=");
		int nextLEnd = nextLString.indexOf("&");
		String totalPage = nextLString.substring(nextLBegin + 8, nextLEnd);
		this.intTypeTotalPage = Integer.parseInt(totalPage);
		
		return valueList;
	}
	
}
